﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.Utils.Filtering;
using DevExpress.XtraReports.UI;
using EyFramework.Common;
using EyFramework.Library;
using EyFramework.ORM;
using EyFramework.Sys.Core;
using Insurance.Core.Service;
using Insurance.Entity.Dto;
using Insurance.ReadCard;

namespace Insurance.Forms
{
    public partial class FrmInsuVoucher : FrmDevBase
    {
        private List<GridColumnInfo> columnInfos = new List<GridColumnInfo>();

        private IInsuVoucherService insuVoucherService;
        //private IHzybDzpzDyjlService hzybDzpzDyjlService;

        public FrmInsuVoucher()
        {
            InitializeComponent();
        }

        private void btnDzpz_Click(object sender, EventArgs e)
        {
            ScanDzpz();
        }

        private void ScanDzpz()
        {
            ////读取医保电子凭证信息
            //HzybConstants.GetYeWuZqh("000HIS");
            var cardInfo = ReadCardService.ReadDzpzCard();
            //根据身份证号获取当天的刷码记录
            InsuVoucherParam insuVoucherParam = new InsuVoucherParam()
            {
                PAT_NAME = teXingming.Text.Trim(),
                START_DT = dtDayin.DateTime.ToString("yyyy-MM-dd"),
                END_DT = dtDayin.DateTime.ToString("yyyy-MM-dd"),
                ID_NO = cardInfo.ShenFenZh
            };
            List<InsuVoucherDto> insuVoucherDtos = insuVoucherService.QueryVoucher(insuVoucherParam);
            if (insuVoucherDtos.Count >= 0)
            {
                if (Globals.ShowYesNoAndWarning("当日已经刷电子凭证，是否重复刷？") != DialogResult.Yes)
                {
                    return;
                }
            }
            ShowLoading();
            try
            {
                ////读取杭州医保卡信息
                //var insuAccountParam = new InsuAccountParam(cardInfo, "", "", "", "", true);
                //var ybicinfo = (HzybAccount)YbService.GetInsuAccount(insuAccountParam);
                //ybicinfo.BingRenId = ncovOrderRecord.CHT_ID;
                ////调用挂号结算
                //var ghjs = GuaHaoJiaoYi(ybicinfo);
                ////根据身份证号，获取体检号码，打印最后一次未打印的体检条码
                ////调用收费结算
                //var sfjs = ShouFeiJiaoYi(ybicinfo);
            }
            catch (Exception ex)
            {
                Globals.ShowError("电子凭证刷码失败:" + ex.ToString());
                return;
            }
            finally
            {
                CloseLoading();
            }
            QueryData();
        }

        private void InitForm()
        {
            gvData.ShowRowNum();
            gcData.ShowContextMenu(this.components);//显示右键导出功能
            #region
            columnInfos.Add(new GridColumnInfo("PAT_NAME", "姓名", 100));
            columnInfos.Add(new GridColumnInfo("SEX", "性别", 100));
            columnInfos.Add(new GridColumnInfo("ID_NO", "身份证号", 200));
            columnInfos.Add(new GridColumnInfo("CARD_NO", "医保卡号", 200));
            columnInfos.Add(new GridColumnInfo("CARD_ID", "卡识别码", 100));
            columnInfos.Add(new GridColumnInfo("PSN_NO", "个人编号", 200));
            columnInfos.Add(new GridColumnInfo("KESHIMC", "科室名称", 100));
            columnInfos.Add(new GridColumnInfo("INSU_DATE", "刷码日期", 200));
            columnInfos.Add(new GridColumnInfo("CREATE_BY", "操作员", 100));
            #endregion
        }

        private void QueryData()
        {
            InsuVoucherParam insuVoucherParam = new InsuVoucherParam()
            {
                PAT_NAME = teXingming.Text.Trim(),
                START_DT = dtDayin.DateTime.ToString("yyyy-MM-dd"),
                END_DT = dtDayin.DateTime.ToString("yyyy-MM-dd")
            };
            List<InsuVoucherDto> voucherDtos = insuVoucherService.QueryVoucher(insuVoucherParam);
            gcData.DataSource = voucherDtos;
            DevPublic.SetGridControlView(gvData, columnInfos, true, true);
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            QueryData();
        }

        private void 补打条码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //if (gvData.FocusedRowHandle >= 0)
            //{
            //    var dzpzBarInfo = gvData.GetRow(gvData.FocusedRowHandle) as DzpzBarInfo;
            //    NcovOrderRecord ncovOrderRecord = new NcovOrderRecord()
            //    {
            //        BAR_NO = dzpzBarInfo.TIAOMAHAO,
            //        PAT_NM = dzpzBarInfo.PAT_NM,
            //        CHT_ID = dzpzBarInfo.BINGRENID
            //    };
            //    PrintLab(ncovOrderRecord);
            //}
        }

        private void FrmHzybDzpz_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F1)
            {
                btnDzpz_Click(btnDzpz, e);
            }
        }

        private void FrmInsuVoucher_Load(object sender, EventArgs e)
        {
            InitForm();
            insuVoucherService = Globals.GetService<IInsuVoucherService>();
            dtDayin.Text = insuVoucherService.GetCurrentDate("YYYY-MM-DD");
            QueryData();
        }
    }
}